pp108 : Retrying a Transaction

Retrying a Transaction

This topic describes the activity of retrying a transaction.


WS-AppServer supports handling multiple transactions at the same time. In this context, it is possible that two or more processes may try to update the same object simultaneously. To address this, WS-AppServer adopts the concept of optimistic transactions, in which it checks the objects in the database for any interim change that may have happened, and subsequently commits the transaction.

Always, the changes from the first transaction are committed to the database without any difficulty. When the following transaction tries to commit the updates, it receives a BSFObjectChangedException , indicating the changed state of the object. Normally, whenever such an exception occurs, the transaction aborts.

This condition is more evident in case of update requests, where the request contains both the old and new tuples. When a BsfObjectChangedException is thrown, the original data of the object (<tuple><old>) is retrieved (using the object.getOriginalObject() method) and is updated with the actual data from the database. As a result, the old object in the request attains the same status as that of the object in database. Again, an attempt to commit the transaction is made. This time, the commit would be successful.
Based on the content of the original object, you can take necessary steps. You have the following options:

  • In case of an update that is not conflicting, commit the transaction again by using the com.cordys.cpc.bsf.busobject.BSF class commitTransaction() method. The commit will be successful because the original object data (<tuple><old>) will now be according to the content of the database.
  • Modify the object based on the content of the original object, commit the transaction by using the com.cordys.cpc.bsf.busobject.BSF class commitTransaction() method. The commit will be successful because the original object data (<tuple><old>) is now according to the content of the database.
  • If it is a conflict does not solve on its own, abort the transaction using com.cordys.cpc.bsf.busobject.BSF class abortTransaction() method , and display an error to the user.
    Using WS-AppServer, a transaction can be programmed to make repetitive attempts to commit inspite of such failures and exceptions. The following code is a sample of how you can achieve this.
            String transactionID = Native.createGuid();
    	BSF.getObjectManager().setMaxRetry(5);
    	Region region = null;
    	do
    	{
    		try
    		{
    			BSF.startTransaction(transactionID);
    			region = Region.getRegionObject(1);
    			region.setRegionDescription(name);
    			region.update();
    			BSF.commitTransaction();
    		}
    		catch (Exception e)
    		{
    			if(e instanceof BsfRetriableException)
    			{
    			  BSF.setRetry(transactionID,(BsfObjectChangedException)e);
    			}
    			e.printStackTrace();
    		}
    	}while(BSF.retryTransaction(transactionID));
    


    Note: The methods setRetry(String transactionID) and retryTransaction(String transactionID) are instance methods on BusObjectManager. The static WS-AppServer methods map to the BusObjectManager bound to the current that is, thread bound, BsfContext. For details on the methods used in this process, refer WS-AppServer SDK.

Related concepts

Parallel Transactions

Related reference

Starting a Transaction

Related information

Managing Transactions Using WS-AppServer